﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Common;
using WebSite.Core;

namespace WebSite.Server
{
    /// <summary>
    /// AddComment 的摘要说明
    /// </summary>
    public class AddComment : IHttpHandler, System.Web.SessionState.IRequiresSessionState
    {

        public void ProcessRequest( HttpContext context )
        {
            context.Response.ContentType = "text/json";
            RetInfo response = RetInfoHelper.WebServiceReturn( delegate()
            {
                if (Project_Session.CurrentUser == null || Project_Session.CurrentUserBaby == null)
                {
                    throw new RuntimeException( 3, "请先完善宝宝资料" );
                }

                if (NullableHelper.CastValueType( Project_Session.CurrentUserBaby.ConfirmTeamId ) == 0)
                {
                    throw new RuntimeException( 10, "您的宝宝尚未组队，组队后才能留言" );
                }

                string content = StringHelper.Convert( context.Request["content"], "" );

                if (string.IsNullOrEmpty( content ) || content.Trim() == "")
                {
                    throw new RuntimeException( 301, "请填写留言内容" );
                }

                if (content.Length>=200)
                {
                    throw new RuntimeException( 301, "留言内容不能超过200个字" );
                }


                Maticsoft.BLL.CarrefourCover.Message bll = new Maticsoft.BLL.CarrefourCover.Message();
                bll.Add( new Maticsoft.Model.CarrefourCover.Message()
                {
                    SendBabyId = Project_Session.CurrentUserBaby.Id,
                    SendBabyName = Project_Session.CurrentUserBaby.BabyName,
                    SendHeadImg = Project_Session.CurrentUserBaby.BabyHeadImg,
                    Content = content,
                    ReceviedTeamId = Project_Session.CurrentUserBaby.ConfirmTeamId,
                    MessageType = 0,
                    Ip = context.Request.UserHostAddress,
                    CreateTime = DateTime.Now
                } );

                return true;
            } );

            context.Response.Write( response.SerializeToString( Common.Serialization.SerializationType.Json ) );
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}